//package com.haircut.filter;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.jwt.JWTPayload;
//import cn.hutool.jwt.JWTUtil;
//import com.haircut.entity.WechatUser;
//import com.haircut.security.constants.SecurityConstants;
//import com.haircut.service.WechatUserService;
//import jakarta.servlet.http.HttpServletRequest;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpHeaders;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.filter.CommonsRequestLoggingFilter;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.concurrent.TimeUnit;
//
///**
// * 请求日志打印过滤器
// *
//
// * @since 2023/03/03
// */
//@Configuration
//@Slf4j
//public class RequestLogFilter extends CommonsRequestLoggingFilter {
//
//    @Autowired
//    private WechatUserService wechatUserService;
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//    private static final long FIVE_MINUTES_IN_MILLIS = 5 * 60 * 1000;
//    @Override
//    protected boolean shouldLog(HttpServletRequest request) {
//        // 设置日志输出级别，默认debug
//        return this.logger.isInfoEnabled();
//    }
//
//    @Override
//    protected void beforeRequest(HttpServletRequest request, String message) {
//        String requestURI = request.getRequestURI();
//        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
//        super.beforeRequest(request, message);
//    }
//
//    @Override
//    protected void afterRequest(HttpServletRequest request, String message) {
//        String requestURI = request.getRequestURI();
//        String bearerToken = request.getHeader(HttpHeaders.AUTHORIZATION);
//
//        if (StrUtil.isNotBlank(bearerToken) && bearerToken.startsWith("Bearer ")) {
//            try {
//                // 移除 "Bearer " 前缀，从令牌中提取用户信息(username), 并设置到认证信息中
//                bearerToken = bearerToken.substring(SecurityConstants.JWT_TOKEN_PREFIX.length());
//                String openId = JWTUtil.parseToken(bearerToken).getPayloads().getStr(JWTPayload.SUBJECT);
//                WechatUser wechatUser = wechatUserService.getByOpenid(openId);
//                String userId = wechatUser.getId().toString();
//
//                // 检查是否为 GET 请求
//                if ("GET".equalsIgnoreCase(request.getMethod())) {
//                    long currentTime = System.currentTimeMillis();
//                    String key = getRedisKey(userId, requestURI);
//                    Object lastRequestTimeStr = redisTemplate.opsForValue().get(key);
//                    if (lastRequestTimeStr == null) {
//                        redisTemplate.opsForValue().set(key, String.valueOf(currentTime), 5, TimeUnit.MINUTES);
//                    } else {
//                        long lastRequestTime = Long.parseLong(lastRequestTimeStr.toString());
//                        if (currentTime - lastRequestTime > FIVE_MINUTES_IN_MILLIS) {
//                            redisTemplate.opsForValue().set(key, String.valueOf(currentTime));
//                        }
//                    }
//                }
//
//                log.info("requestURI:{},userId:{}", requestURI, wechatUser.getId());
//            } catch (Exception e) {
//                log.error("Failed to process authentication token.", e);
//            }
//        }
//        super.afterRequest(request, message);
//    }
//    private String getRedisKey(String userId, String url) {
//        return "user:" + userId + ":url:" + url;
//    }
//
//}
